package com.maoye.ossp.sys.site.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.ossp.sys.site.entity.SiteSkin;
import com.maoye.ossp.sys.site.service.ISiteSkinService;

@Controller
@RequestMapping(value = "/sys/site/skin")
public class SiteSkinController extends BaseController{
	
	@Resource
	private ISiteSkinService iSiteSkinService;
	
	/**
	 * 获得所有信息展示
	 * @param model
	 * @param request
	 * @param siteSkin
	 * @return
	 * @data 2014年7月17日
	 */
	@RequestMapping(value = "/list")
	public String getList(Model model,HttpServletRequest request,SiteSkin siteSkin){
		QueryFilter queryFilter = getQuerFilter(request,siteSkin);
		Page<SiteSkin> page = (Page<SiteSkin>)iSiteSkinService
				.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/site/skin/list", model, request);
	}
	
	@RequestMapping(value = "/addSiteSkin")
	public String addSiteSkin(Model model){
		SiteSkin siteSkin = new SiteSkin();
		model.addAttribute("siteSkin", siteSkin);
		return "/sys/site/skin/add";
	}
	
	/**
	 * 保存皮肤信息
	 * @param siteSkin
	 * @param redirectAttributes
	 * @return
	 * @data 2014年7月17日
	 */
	@RequestMapping(value = "/saveSiteSkin")
	public String saveLink(SiteSkin siteSkin,HttpServletRequest request,RedirectAttributes redirectAttributes){
		siteSkin.setId(idGenerator.getUId());
		siteSkin.setCreateBy(UserUtils.getCurrentUserId().toString());
		siteSkin.setUpdateBy("");
		if("0".equals(siteSkin.getIsDefault())){
			iSiteSkinService.saveSiteSkin(siteSkin);
		}
		else{
			iSiteSkinService.updateAll(siteSkin);
			iSiteSkinService.saveSiteSkin(siteSkin);
		}	
		addMessage(redirectAttributes, "保存成功");
		return "redirect:/sys/site/skin/list";
	}
	
	/**
	 * 查询详细信息
	 * @return
	 * @data 2014年7月17日
	 */
	@RequestMapping(value = "/viewSkin")
	public String view(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		SiteSkin siteSkin = iSiteSkinService.getSiteSkinById(id);
		model.addAttribute("siteSkin",siteSkin);
		return "/sys/site/skin/view";
	}
	
	@RequestMapping(value = "/updateSkin")
	public String updateSkin(Model model,HttpServletRequest request){
		String id = request.getParameter("id");
		SiteSkin siteSkin = iSiteSkinService.getSiteSkinById(id);
		model.addAttribute("siteSkin", siteSkin);
		return "/sys/site/skin/update";
	}
	
	/**
	 * 修改皮肤信息
	 * @param siteSkin
	 * @param redirectAttributes
	 * @return
	 * @data 2014年7月18日
	 */
	@RequestMapping(value = "/saveUpdateSkin")
	public String saveUpdateSkin(SiteSkin siteSkin,RedirectAttributes redirectAttributes){
		siteSkin.setUpdateBy(UserUtils.getCurrentUserId().toString());
		if("0".equals(siteSkin.getIsDefault())){
			iSiteSkinService.saveUpdateSkin(siteSkin);
		}
		else{
			iSiteSkinService.updateAll(siteSkin);
			iSiteSkinService.saveUpdateSkin(siteSkin);
		}
		addMessage(redirectAttributes, "修改成功");
		return "redirect:/sys/site/skin/list";
	}
	
	/**
	 * 删除皮肤信息
	 * @param request
	 * @param redirectAttributes
	 * @return
	 * @data 2014年7月18日
	 */
	@RequestMapping(value = "/removeSkin")
	public String removeSkin(HttpServletRequest request,RedirectAttributes redirectAttributes){
		String id = request.getParameter("id");
		iSiteSkinService.deleteSkin(id);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:/sys/site/skin/list";
	}
	
	/**
	 * 查询所有信息至前端
	 * @param request
	 * @param response
	 * @throws IOException
	 * @data 2014年7月18日
	 */
	@RequestMapping(value = "/getAllSkin")
	public void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		List<SiteSkin> list = iSiteSkinService.getAllSkinInfo(); 
        PrintWriter out=response.getWriter();  
        JSONArray jsonArray=JSONArray.fromObject(list);  
        out.print(jsonArray);  
        out.flush();  
        out.close();  
	}
	
	/**
	 * 检查皮肤包是否存在
	 * @param request
	 * @param response
	 * @data 2014年7月22日
	 */
	@RequestMapping(value = "/checkSkin")
	public void checkSkin(HttpServletRequest request,HttpServletResponse response) throws IOException{
		Map map = new HashMap();
		String skin = request.getParameter("skin");	
		map.put("skin",skin);
		PrintWriter out = response.getWriter();
		JSONObject json = new JSONObject();
		if(iSiteSkinService.checkSkin(map)){
			json.put("skin",0); //允许添加新皮肤包
		}else{
			json.put("skin",1);//皮肤包已存在
		}
		out.print(json.toString());
		out.flush();
		out.close();
	}
	
	
}
